import modelExtend from 'dva-model-extend'
import { lwjPageModel } from 'utils/model'
import { listPaymenters, findReceipter, listPaymentPattern, checkPaymentNum } from './services/financialReceipt'
import { create } from './services/financialReceipt';
import { routerRedux } from 'dva/router';

export default modelExtend(lwjPageModel, {

  namespace: 'receiptAdd',
  state: {
    currentItem: {},
    isShowSelect: false,
    currentRecord: "",
    onSelectedReceipter: undefined,
    queryParam: {},
    detailUrl: "",
    paymentPattern: {},
    paymentNumStatus: "",
    pageOptions: {
      sort: 'createTime',
      order: 'DESC'
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/receipt/add') {
          dispatch({ type: 'query', payload: {} })
          dispatch({
            type: 'listPaymentPattern'
          })
        }
      })
    }
  },
  effects: {
    * query({
      payload
    }, { select, call, put }) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      });
      const pageOptions = yield select(({ receiptAdd }) => receiptAdd.pageOptions);
      delete payload.isTablePagination
      const queryPayload = {
        ...pageOptions,
        ...payload
      };
      const data = yield call(listPaymenters, queryPayload);
      const receipter = yield call(findReceipter, payload);
      yield put({
        type: 'querySuccess',
        payload: {
          ...data,
        }
      })
      yield put({
        type: 'getReceipterSuccess',
        payload: {
          ...data,
          ...receipter.result,
        }
      })
    },
    * create({
      payload
    }, { select, call, put }) {
      const data = yield call(create, payload)
      if (data.code === 0) {
        yield put({
          type: 'setDetailUrl',
          payload: {
            id: data.result
          }
        })
        yield put({
          type: 'showSuccess',
          payload: {}
        })

      }
    },
    * showSuccess({
      payload
    }, { select, call, put }) {
      const detailUrl = yield select(({ receiptAdd }) => receiptAdd.detailUrl);
      yield put(routerRedux.push('/success', { listUrl: '/receipt', detailUrl: detailUrl }));
    },
    * listPaymentPattern({
      payload
    }, { select, call, put }) {
      const data = yield call(listPaymentPattern);
      yield put({
        type: 'setPaymentPattern',
        payload: {
          paymentPattern: data.result
        }
      })
    },
    * checkPaymentNum({
      payload
    }, { select, call, put }) {
      const data = yield call(checkPaymentNum, payload);
      yield put({
        type: 'setPaymentNumStatus',
        payload: {
          paymentNumStatus: data.result ? undefined : "支付凭证号已经存在"
        }
      })
    },


  },

  reducers: {
    asyncData(state, { payload }) {
      return {
        ...state,
        queryParam: {
          ...payload
        }
      }
    },
    showModalDetail(state, { payload }) {
    },
    isShowSelect(state, { payload }) {
      return {
        ...state,
        isShowSelect: payload.visible
      }
    },
    getReceipterSuccess(state, { payload }) {
      return {
        ...state,
        onSelectedReceipter: undefined,
        currentRecord: undefined,
        currentReceipter: payload,
        currentReceipterAccounts: payload.allFinancialAccount
      }
    },
    updateReceipter(state, { payload }) {
      return {
        ...state,
        onSelectedReceipter: payload.receipter
      }
    },
    updateCurrentRecord(state, { payload }) {
      return {
        ...state,
        currentRecord: payload.record,
      }
    },
    setDetailUrl(state, { payload }) {
      return {
        ...state,
        detailUrl: "/receipt/detail/" + payload.id,
      }
    },
    setPaymentPattern(state, { payload }) {
      return {
        ...state,
        paymentPattern: payload.paymentPattern,
      }
    },
    setPaymentNumStatus(state, { payload }) {
      return {
        ...state,
        paymentNumStatus: payload.paymentNumStatus,
      }
    },
  }
})
